﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "新增登记";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    var keyValue = $.request("keyValue");
    var sfzh;
    var age;
    var vcity = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    };
    $(function () {
        init();
        if (!!keyValue) {
            $("#blh").val(keyValue);
        }
        $('#zjh').bind("keydown blur",
            function (e) {
                if (((e.keyCode === 13 && e.type === "keydown") || e.type === "blur") && $('#zjlx').val() === "1") {
                    var len = $(this).val().length;
                    var sfzh = $(this).val();
                    if (len === 15 || len === 18) {
                        var csrq;
                        csrq = sfzh.substr(6, 8);
                        if ((len === 15 && csrq) || len === 18) {
                            csrq = csrq.replace(/(.{4})(.{2})/, "$1-$2-");
                            //获取性别
                            var xb;
                            if (parseInt(sfzh.charAt(16) / 2) * 2 != sfzh.charAt(16))
                                xb = 1;
                            else
                                xb = 0;
                            //获取年龄
                            var myDate = new Date();
                            var month = myDate.getMonth() + 1;
                            var day = myDate.getDate();
                            age = myDate.getFullYear() - sfzh.substring(6, 10) - 1;
                            if (sfzh.substring(10, 12) < month ||
                                sfzh.substring(10, 12) == month && sfzh.substring(12, 14) <= day) {
                                age++;
                            }
                            $('#csny').val(csrq);
                            if (xb == 0) {
                                if ($("input[name=xb]:eq(0)").parent().hasClass('active')) {
                                    $("input[name=xb]:eq(0)").parent().removeClass('active');
                                }
                                $("input[name=xb]:eq(1)").attr("checked", 'checked'); //女
                                $("input[name=xb]:eq(1)").parent().addClass('active');
                            } else {
                                if ($("input[name=xb]:eq(1)").parent().hasClass('active')) {
                                    $("input[name=xb]:eq(1)").parent().removeClass('active');
                                }
                                $("input[name=xb]:eq(0)").attr("checked", 'checked'); //男
                                $("input[name=xb]:eq(0)").parent().addClass('active');
                            }
                            $('#nl').val(age);
                        }
                    }
                }
            });

        //根据姓名获得拼音
        $('#xm').blur(function () {
            $('#py').val($(this).toPinyin());

        });
        $("#jsr").keyupEnterEvent(function () {
            submitForm();
        });
    });

    function init() {
        //国籍
        $("#gj").newtouchBindSelect({
            id: "gjCode",
            text: "gjmc",
            datasource: function () {
                var resultObjArr = new Array();
                if (top.window.newtouchclients.sysNationalityList) {
                    $.each(top.window.newtouchclients.sysNationalityList,
                        function (idx, val) {
                            resultObjArr.push(val);
                        });
                }
                return resultObjArr;
            }
        });

    }

    function submitForm() {
        var data = $("#form1").formSerialize();
        if (AddData_Check()) {
            $.submitForm({
                url: "/PatientManage/HospiterRes/SubmitForm?addmzInfo=true",
                param: data,
                success: function () {
                    $.loading(false);
                    if ($.currentWindow().$("#blh").length === 1) {
                        //门诊挂号免卡登记
                        $.currentWindow().searchPatInfo(data.blh);
                        $.modalClose();
                    }
                }
            });
        }
    }

    checkCard = function () {
        var card = $("#zjh").val().toUpperCase();
        $("#zjh").val(card);
        if ($('#zjlx').val() !== "1") {
            if (card === '') {
                $.modalAlert('请输入证件号，证件号不能为空', 'warning');
                return false;
            }
            return true;
        }

        //是否为空
        if (card === '') {
            $.modalAlert('请输入身份证号，身份证号不能为空', 'warning');
            // $("#zjh").focus;
            return false;
        }
        //校验长度，类型
        if (isCardNo(card) === false) {
            $.modalAlert('您输入的身份证号码不正确，请重新输入', 'warning');
            //$("#zjh").focus;
            return false;
        }
        //检查省份
        if (checkProvince(card) === false) {
            $.modalAlert('您输入的身份证前两位不正确,请重新输入', 'warning');
            //$("#zjh").focus;
            return false;
        }
        //校验生日
        if (checkBirthday(card) === false) {
            $.modalAlert('您输入的身份证号码生日不正确,请重新输入', 'warning');
            //$("#zjh").focus();
            return false;
        }
        //检验位的检测
        if (checkParity(card) === false) {
            $.modalAlert('您的身份证校验位不正确,请重新输入', 'warning');
            // $("#zjh").focus();
            return false;
        }
        return true;
    };


    //检查号码是否符合规范，包括长度，类型
    isCardNo = function (card) {
        //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
        var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
        if (reg.test(card) === false) {
            return false;
        }

        return true;
    };

    //取身份证前两位,校验省份
    checkProvince = function (card) {
        var province = card.substr(0, 2);
        if (vcity[province] == undefined) {
            return false;
        }
        return true;
    };

    //检查生日是否正确
    checkBirthday = function (card) {
        var len = card.length;
        //身份证15位时，次序为省（3位）市（3位）年（2位）月（2位）日（2位）校验位（3位），皆为数字
        if (len == '15') {
            var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
            var arr_data = card.match(re_fifteen);
            var year = arr_data[2];
            var month = arr_data[3];
            var day = arr_data[4];
            var birthday = new Date('19' + year + '/' + month + '/' + day);
            return verifyBirthday('19' + year, month, day, birthday);
        }
        //身份证18位时，次序为省（3位）市（3位）年（4位）月（2位）日（2位）校验位（4位），校验位末尾可能为X
        if (len == '18') {
            var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
            var arr_data = card.match(re_eighteen);
            var year = arr_data[2];
            var month = arr_data[3];
            var day = arr_data[4];
            var birthday = new Date(year + '/' + month + '/' + day);
            return verifyBirthday(year, month, day, birthday);
        }
        return false;
    };

    //校验日期
    verifyBirthday = function (year, month, day, birthday) {
        var now = new Date();
        var now_year = now.getFullYear();
        //年月日是否合理
        if (birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) {
            //判断年份的范围（3岁到100岁之间)
            var time = now_year - year;
            if (time >= 3 && time <= 100) {
                return true;
            }
            return false;
        }
        return false;
    };

    //校验位的检测
    checkParity = function (card) {
        //15位转18位
        card = changeFivteenToEighteen(card);
        sfzh = card;
        var len = card.length;
        if (len == '18') {
            var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            var cardTemp = 0, i, valnum;
            for (i = 0; i < 17; i++) {
                cardTemp += card.substr(i, 1) * arrInt[i];
            }
            valnum = arrCh[cardTemp % 11];
            if (valnum == card.substr(17, 1)) {
                return true;
            }
            return false;
        }
        return false;
    };

    //15位转18位身份证号
    changeFivteenToEighteen = function (card) {
        if (card.length == '15') {
            var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            var cardTemp = 0, i;
            card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
            for (i = 0; i < 17; i++) {
                cardTemp += card.substr(i, 1) * arrInt[i];
            }
            card += arrCh[cardTemp % 11];
            return card;
        }
        return card;
    };

    function AddData_Check() {
        var blh = $("#blh").val();
        if (blh == null || blh === "" || blh == undefined) {
            $.modalAlert("请填写病历号", 'warning');
            $("#blh").focus();
            return false;
        }
        var xm = $("#xm").val();
        if (xm == null || xm === "" || xm == undefined) {
            $.modalAlert("请填写姓名", 'warning');
            $("#xm").focus();
            return false;
        }
        var zjlx = $("#zjlx").val();
        if (zjlx == null || zjlx === "" || zjlx == undefined) {
            $.modalAlert("请选择证件类型", 'warning');
            $("#zjlx").focus();
            return false;
        }
        //var zjh = $("#zjh").val();
        ////暂时去除身份验证----liixn 20190816
        //if (!checkCard()) {
        //    return false;
        //}
        var csny = $("#csny").val();
        if (csny == null || csny === "" || csny == undefined) {
            $.modalAlert("请填写出生年月", 'warning');
            $("#csny").focus();
            return false;
        }

        var nl = $("#nl").val();
        if (nl == null || nl === "" || nl == undefined) {
            $.modalAlert("请填写年龄", 'warning');
            $("#nl").focus();
            return false;
        }

        //性别
        var xb = false;
        $('input[name="xb"]').each(function () {
            var $this = $(this);
            if ($this.parent().hasClass("active")) {
                xb = true;
                $("#xb").val();
            }
        });
        if (!xb) {
            $.modalAlert("请选中性别！", 'warning');
            return false;
        }
        return true;
    }

    /*根据出生日期算出年龄*/
    function jsGetAge(strBirthday) {
        var returnAge;
        var strBirthdayArr = strBirthday.split("-");
        var birthYear = strBirthdayArr[0];
        var birthMonth = strBirthdayArr[1];
        var birthDay = strBirthdayArr[2];

        d = new Date();
        var nowYear = d.getFullYear();
        var nowMonth = d.getMonth() + 1;
        var nowDay = d.getDate();

        if (nowYear == birthYear) {
            returnAge = 0;//同年 则为0岁
        }
        else {
            var ageDiff = nowYear - birthYear; //年之差
            if (ageDiff > 0) {
                if (nowMonth == birthMonth) {
                    var dayDiff = nowDay - birthDay;//日之差
                    if (dayDiff < 0) {
                        returnAge = ageDiff - 1;
                    }
                    else {
                        returnAge = ageDiff;
                    }
                }
                else {
                    var monthDiff = nowMonth - birthMonth;//月之差
                    if (monthDiff < 0) {
                        returnAge = ageDiff - 1;
                    }
                    else {
                        returnAge = ageDiff;
                    }
                }
            }
            else {
                returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
            }
        }
        $("#nl").val(returnAge);
    }
</script>
<style>
    body {
        overflow: hidden;
    }

    .formTitle span {
        color: red;
    }

    .tab-content #basicInfo table tr td {
        border: 0;
    }
</style>
<form id="form1">
    <input type="hidden" id="py" name="py" />
    <table class="form" style="width: 98%; border: 0; margin-top: 10px">
        <tr>
            <td class="formTitle"><span class="required">*</span>病历号：</td>
            <td class="formValue">
                <input type="text" id="blh" class="form-control form-an ">
            </td>
            <td class="formTitle"><span>*</span>病人姓名：</td>
            <td class="formValue">
                <input type="text" id="xm" name="xm" class="form-control form-an ">
            </td>
            <td class="formTitle">手机：</td>
            <td class="formValue">
                <input type="text" id="phone" name="phone" class="form-control form-an ">
            </td>
        </tr>
        <tr>
            <td class="formTitle"><span>*</span>证件类型：</td>
            <td class="formValue">
                @Html.DropDownList("zjlx", EnumZJLX.sfz.ToDescSelectList(), "==请选择==", new { @class = "form-control" })
            </td>
            <td class="formTitle"><span>*</span>证件号：</td>
            <td class="formValue">
                <input type="text" id="zjh" name="zjh" class="form-control form-an ">
            </td>
            <td class="formTitle"><span>*</span>出生年月：</td>
            <td class="formValue">
                <input id="csny" type="text" class="form-control form-an  input-wdatepicker" onfocus="WdatePicker({
    onpicked: function () {jsGetAge($(this).val());return true;}})" />
            </td>
        </tr>
        <tr>
            <td class="formTitle"><span>*</span>年龄：</td>
            <td class="formValue">
                <input type="text" id="nl" name="nl" class="form-control form-an ">
            </td>
            <td class="formTitle"><span>*</span>性别：</td>
            <td class="formValue">
                <div class="btn-group" data-toggle="buttons">
                    <label class="btn btn-default">
                        <input type="radio" id="xb" value="1" name="xb" class="form-control form-an ">
                        男
                    </label>
                    <label class="btn btn-default">
                        <input type="radio" id="xb" value="0" name="xb" class="form-control form-an ">
                        女
                    </label>
                </div>
            </td>
            <td class="formTitle">介绍人：</td>
            <td class="formValue">
                <input type="text" id="jsr" name="jsr" class="form-control form-an form-an-end">
            </td>
        </tr>
    </table>
</form>
